/**
 * @file Define crawler utility functions
 * 
 * @author Steve Gao<gaofu@parllay.com>
 */

const cheerio       = require('cheerio');
const request       = require('request');

const BaseService   = require('./base-service').BaseService;

class CrawlerService extends BaseService {

    fetchContent(url, callback) {
        request(url, (error, response, body) => {
            if (error) {
                callback(url);
            } else {
                callback(body);
            }
        });
    }

    fetchWeChatArticle(url, callback) {
        this.fetchContent(url, (html) => {
            if (html && html.indexOf('<') >= 0 && html.indexOf('>') >= 0) {
                html = html.replace(/data-src/g, 'src');
                var $ = cheerio.load(html);
                var article = {};
                article.title = $("title").text();
                var images = $(".rich_media img");
                if (images.length > 0) {
                    for(var i = 0; i < images.length; i++) {
                        if (Number($(images[i]).attr('data-w'))> 200) {
                            article.picurl = $(images[i]).attr('src');
                        }
                        if (article.picurl) break;
                    }
                }

                if (!article.picurl) {
                    article.picurl = '//cdn2.parllay.cn/images/default_wall_background.png';
                }
                article.url = url;
                callback(article);
            } else {
                callback(null);
            }
        });
    }
}

exports.CrawlerService = CrawlerService;